package com.nexgo.payment.util;

import android.content.Context;
import android.widget.Toast;

/**
 * Created by xiaox on 2017/3/2.
 */

public class ToastUtils {
    private static String oldMsg;
    private static Toast toast = null;
    private static long oneTime = 0;
    private static long twoTime = 0;
    private static Context mContext;

    public static void init(Context mContext) {
        ToastUtils.mContext = mContext.getApplicationContext();
    }

    public static void showToast(int resId) {
        showToast(mContext.getString(resId));
    }

    public static void showToast(int resId, int gravity) {
        showToast(mContext.getString(resId), gravity, 0, 0);
    }

    public static void showToast(String msg, int gravity) {
        showToast(msg, gravity, 0, 0);
    }

    public static void showToast(int resId, int gravity, int offX, int offY) {
        showToast(mContext.getString(resId), gravity, offX, offY);
    }

    public static void showToast(String msg) {
        if (toast == null) {
            toast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT);

            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (msg.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = msg;
                toast.setText(msg);
                toast.show();
            }
        }
        oneTime = twoTime;
    }

    public static void showToast(String msg, int gravity, int offX, int offY) {
        if (toast == null) {
            toast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT);
            toast.setGravity(gravity, offX, offY);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (msg.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = msg;
                toast.setText(msg);
                toast.show();
            }
        }
        oneTime = twoTime;
    }
}
